package com.cy.pj.goods.pojo;

import com.cy.pj.goods.dao.Goods;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class GoodsDaoImpl implements GoodsDao{
    @Autowired
    private SqlSession sqlSession;//SqlSessionTempLate(线程安全)
    @Override
    public List<Goods> findGoods() {//在此方法内部使用sqlsession实现与数据库的会话

        System.out.println("===findGoods()===");
        String statement = "com.cy.pj.goods.pojo.GoodsDao.findGoods";//namespace+name   将这个作为key
        return sqlSession.selectList(statement);

    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }

    @Override
    public int deleteObjects(Integer... array) {
        String statement = "com.cy.pj.goods.pojo.GoodsDao.deleteObjects";
        Map<String,Object> map = new HashMap<>();
        map.put("ids", array);
        //return sqlSession.delete(statement,ids);//不允许直接这样传值
        //一般有多个参数或者是可变参数时，这个参数要封装到map，然后再进行传递
        //map中的key一般为映射文件中sql中需要的参数
        return sqlSession.delete(statement,map);
    }
}
